import {
    createRecord,
    showLoadingDialog,
    hideLoadingDialog
} from '../views/accountInquiryView.js';
import {} from '../base/observer.js';
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var cw = require("../components/winLocation.js");
var token, loginName, openid, loginType;
var errorObject;
var recordScroll;
var pageNum = 1,
    pageSize = 20;
var $wrapper;
var cellsEmpty = false;
$(document).on("pageinit", "#accountInquiry_page", controllerInit);

var $cells;
// 交易查询控制器初始化
function controllerInit() {
    cw.checkBrowser();
    initValue();    
    createSubscribe();
    getRecordData();
    $("#fanhui").on("touchstart", function() {
        location.href = "./merchantHome.html";
    });
    initIscroll();



}

// 订阅可以创建交易记录信息
function createSubscribe() {

    $.subscribe("recordDataback", function(e, recordObjets) {
        createRecord(recordObjets, $cells);
    });
}


// 取交易记录数据
function getRecordData() {
    showLoadingDialog();
    var asrParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType,
        "pageNum": pageNum,
        "pageSize": pageSize
    }
    $.post(AccountSearch_URL, asrParameters, function(recordObjets) {
        hideLoadingDialog();
        if (recordObjets.login_response) {
            haslogin(recordObjets);
            return;
        }
        if(cellsEmpty){
            $cells.empty();
        }
        if (recordObjets.tltdtrans_response && recordObjets.tltdtrans_response.trans_info_arrays && recordObjets.tltdtrans_response.trans_info_arrays.trans_info) {
            var trans_info = recordObjets.tltdtrans_response.trans_info_arrays.trans_info
            createRecord(trans_info, $cells);
            if (recordScroll) {
                recordScroll.refresh();
            }
            if(!trans_info || trans_info.length < 20){
                $wrapper.find('#pullup-label').html("没有更多记录了");
            }else if(trans_info.length == 20){
                $wrapper.find('#pullup-label').html("上拉加载更多...");
            }
        } else if (recordObjets.login_response) {
            errorObject.showErroDialog("登录失效,该账户已在别处登录");
        } else if (recordObjets.tltdtrans_response && recordObjets.tltdtrans_response.resp_msg) {
            errorObject.showErroDialog(recordObjets.tltdtrans_response.resp_msg);
            $wrapper.find('#pullup-label').html("没有更多记录了");
        } else {
            errorObject.showErroDialog("查询交易记录失败");
        }

    });

}

/*-----------------------------------
滚动模块 */

function initIscroll() {
    var pullText = {
        "pulldownRefresh": "下拉刷新...",
        "pullupLoadingMore": '上拉加载更多...',
        "releaseToRefresh": '松手开始刷新...',
        "releaseToLoading": '松手开始加载...',
        "loading": '加载中...'
    };
    setTimeout(function() {
        recordScroll = new IScroll('#iScroll-box');

        //滚动结束之后触发的事件
        recordScroll.on('scrollEnd', function() {
            console.log("dfksjd");
            var $pulldown = $wrapper.find('#pulldown'),
                $pullup = $wrapper.find('#pullup');

            if ($pulldown.length > 0 && this.y >= 0) {
                $pulldown.removeClass().addClass('loading');
                $pulldown.find('#pulldown-label').html(pullText.releaseToRefresh);
                $wrapper.find('#pullup-label').html(pullText.pullupLoadingMore);
                if (typeof pullDownAction === 'function') {
                    pullDownAction.call(recordScroll);
                }
            } else if ($pulldown.length > 0 && this.y < -5 && this.y > this.maxScrollY + 5) {
                $pullup.find('#pullup-label').html(pullText.pullupLoadingMore);
            } else if ($pullup.length > 0 && this.y <= this.maxScrollY) {
                $pullup.removeClass().addClass('loading');
                $pullup.find('#pullup-label').html(pullText.loading);
                if (typeof pullUpAction === 'function' && $pullup.parent().length > 0) {
                    pullUpAction.call(recordScroll);
                }
            }
        });

    }, 800);
}
/**
 * 下拉刷新
 */
function pullDownAction() {
    console.log("pullDownAction");
    
    cellsEmpty = true;
    pageNum = 1;
    getRecordData();

}

/*上拉加载更多*/
function pullUpAction() {
    pageNum++;
    cellsEmpty = false;
    getRecordData();
}

/* 滚动模块
-------------------------------------------*/

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}


// 初始化变量
function initValue() {
    $cells = $("#record_cells");
    errorObject = new errorDialog();
    getSessionStorage();
    logoutObject = new Logout();
    $wrapper = $('#iScroll-box');
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}